1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.TlsDatabase;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.SocketConnectableIF;
30 private import gio.TlsCertificate;
31 private import gio.TlsInteraction;
32 private import gio.c.functions;
33 public  import gio.c.types;
34 private import glib.ByteArray;
35 private import glib.ErrorG;
36 private import glib.GException;
37 private import glib.ListG;
38 private import glib.Str;
39 private import glib.c.functions;
40 private import gobject.ObjectG;
41 
42 
43 /**
44  * #GTlsDatabase is used to look up certificates and other information
45  * from a certificate or key store. It is an abstract base class which
46  * TLS library specific subtypes override.
47  * 
48  * A #GTlsDatabase may be accessed from multiple threads by the TLS backend.
49  * All implementations are required to be fully thread-safe.
50  * 
51  * Most common client applications will not directly interact with
52  * #GTlsDatabase. It is used internally by #GTlsConnection.
53  *
54  * Since: 2.30
55  */
56 public class TlsDatabase : ObjectG
57 {
58 	/** the main Gtk struct */
59 	protected GTlsDatabase* gTlsDatabase;
60 
61 	/** Get the main Gtk struct */
62 	public GTlsDatabase* getTlsDatabaseStruct(bool transferOwnership = false)
63 	{
64 		if (transferOwnership)
65 			ownedRef = false;
66 		return gTlsDatabase;
67 	}
68 
69 	/** the main Gtk struct as a void* */
70 	protected override void* getStruct()
71 	{
72 		return cast(void*)gTlsDatabase;
73 	}
74 
75 	/**
76 	 * Sets our main struct and passes it to the parent class.
77 	 */
78 	public this (GTlsDatabase* gTlsDatabase, bool ownedRef = false)
79 	{
80 		this.gTlsDatabase = gTlsDatabase;
81 		super(cast(GObject*)gTlsDatabase, ownedRef);
82 	}
83 
84 
85 	/** */
86 	public static GType getType()
87 	{
88 		return g_tls_database_get_type();
89 	}
90 
91 	/**
92 	 * Create a handle string for the certificate. The database will only be able
93 	 * to create a handle for certificates that originate from the database. In
94 	 * cases where the database cannot create a handle for a certificate, %NULL
95 	 * will be returned.
96 	 *
97 	 * This handle should be stable across various instances of the application,
98 	 * and between applications. If a certificate is modified in the database,
99 	 * then it is not guaranteed that this handle will continue to point to it.
100 	 *
101 	 * Params:
102 	 *     certificate = certificate for which to create a handle.
103 	 *
104 	 * Returns: a newly allocated string containing the
105 	 *     handle.
106 	 *
107 	 * Since: 2.30
108 	 */
109 	public string createCertificateHandle(TlsCertificate certificate)
110 	{
111 		auto retStr = g_tls_database_create_certificate_handle(gTlsDatabase, (certificate is null) ? null : certificate.getTlsCertificateStruct());
112 
113 		scope(exit) Str.freeString(retStr);
114 		return Str.toString(retStr);
115 	}
116 
117 	/**
118 	 * Look up a certificate by its handle.
119 	 *
120 	 * The handle should have been created by calling
121 	 * g_tls_database_create_certificate_handle() on a #GTlsDatabase object of
122 	 * the same TLS backend. The handle is designed to remain valid across
123 	 * instantiations of the database.
124 	 *
125 	 * If the handle is no longer valid, or does not point to a certificate in
126 	 * this database, then %NULL will be returned.
127 	 *
128 	 * This function can block, use g_tls_database_lookup_certificate_for_handle_async() to perform
129 	 * the lookup operation asynchronously.
130 	 *
131 	 * Params:
132 	 *     handle = a certificate handle
133 	 *     interaction = used to interact with the user if necessary
134 	 *     flags = Flags which affect the lookup.
135 	 *     cancellable = a #GCancellable, or %NULL
136 	 *
137 	 * Returns: a newly allocated
138 	 *     #GTlsCertificate, or %NULL. Use g_object_unref() to release the certificate.
139 	 *
140 	 * Since: 2.30
141 	 *
142 	 * Throws: GException on failure.
143 	 */
144 	public TlsCertificate lookupCertificateForHandle(string handle, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable)
145 	{
146 		GError* err = null;
147 
148 		auto __p = g_tls_database_lookup_certificate_for_handle(gTlsDatabase, Str.toStringz(handle), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
149 
150 		if (err !is null)
151 		{
152 			throw new GException( new ErrorG(err) );
153 		}
154 
155 		if(__p is null)
156 		{
157 			return null;
158 		}
159 
160 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p, true);
161 	}
162 
163 	/**
164 	 * Asynchronously look up a certificate by its handle in the database. See
165 	 * g_tls_database_lookup_certificate_for_handle() for more information.
166 	 *
167 	 * Params:
168 	 *     handle = a certificate handle
169 	 *     interaction = used to interact with the user if necessary
170 	 *     flags = Flags which affect the lookup.
171 	 *     cancellable = a #GCancellable, or %NULL
172 	 *     callback = callback to call when the operation completes
173 	 *     userData = the data to pass to the callback function
174 	 *
175 	 * Since: 2.30
176 	 */
177 	public void lookupCertificateForHandleAsync(string handle, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
178 	{
179 		g_tls_database_lookup_certificate_for_handle_async(gTlsDatabase, Str.toStringz(handle), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
180 	}
181 
182 	/**
183 	 * Finish an asynchronous lookup of a certificate by its handle. See
184 	 * g_tls_database_lookup_certificate_for_handle() for more information.
185 	 *
186 	 * If the handle is no longer valid, or does not point to a certificate in
187 	 * this database, then %NULL will be returned.
188 	 *
189 	 * Params:
190 	 *     result = a #GAsyncResult.
191 	 *
192 	 * Returns: a newly allocated #GTlsCertificate object.
193 	 *     Use g_object_unref() to release the certificate.
194 	 *
195 	 * Since: 2.30
196 	 *
197 	 * Throws: GException on failure.
198 	 */
199 	public TlsCertificate lookupCertificateForHandleFinish(AsyncResultIF result)
200 	{
201 		GError* err = null;
202 
203 		auto __p = g_tls_database_lookup_certificate_for_handle_finish(gTlsDatabase, (result is null) ? null : result.getAsyncResultStruct(), &err);
204 
205 		if (err !is null)
206 		{
207 			throw new GException( new ErrorG(err) );
208 		}
209 
210 		if(__p is null)
211 		{
212 			return null;
213 		}
214 
215 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p, true);
216 	}
217 
218 	/**
219 	 * Look up the issuer of @certificate in the database. The
220 	 * #GTlsCertificate:issuer property of @certificate is not modified, and
221 	 * the two certificates are not hooked into a chain.
222 	 *
223 	 * This function can block. Use g_tls_database_lookup_certificate_issuer_async()
224 	 * to perform the lookup operation asynchronously.
225 	 *
226 	 * Beware this function cannot be used to build certification paths. The
227 	 * issuer certificate returned by this function may not be the same as
228 	 * the certificate that would actually be used to construct a valid
229 	 * certification path during certificate verification.
230 	 * [RFC 4158](https://datatracker.ietf.org/doc/html/rfc4158) explains
231 	 * why an issuer certificate cannot be naively assumed to be part of the
232 	 * the certification path (though GLib's TLS backends may not follow the
233 	 * path building strategies outlined in this RFC). Due to the complexity
234 	 * of certification path building, GLib does not provide any way to know
235 	 * which certification path will actually be used when verifying a TLS
236 	 * certificate. Accordingly, this function cannot be used to make
237 	 * security-related decisions. Only GLib itself should make security
238 	 * decisions about TLS certificates.
239 	 *
240 	 * Params:
241 	 *     certificate = a #GTlsCertificate
242 	 *     interaction = used to interact with the user if necessary
243 	 *     flags = flags which affect the lookup operation
244 	 *     cancellable = a #GCancellable, or %NULL
245 	 *
246 	 * Returns: a newly allocated issuer #GTlsCertificate,
247 	 *     or %NULL. Use g_object_unref() to release the certificate.
248 	 *
249 	 * Since: 2.30
250 	 *
251 	 * Throws: GException on failure.
252 	 */
253 	public TlsCertificate lookupCertificateIssuer(TlsCertificate certificate, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable)
254 	{
255 		GError* err = null;
256 
257 		auto __p = g_tls_database_lookup_certificate_issuer(gTlsDatabase, (certificate is null) ? null : certificate.getTlsCertificateStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
258 
259 		if (err !is null)
260 		{
261 			throw new GException( new ErrorG(err) );
262 		}
263 
264 		if(__p is null)
265 		{
266 			return null;
267 		}
268 
269 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p, true);
270 	}
271 
272 	/**
273 	 * Asynchronously look up the issuer of @certificate in the database. See
274 	 * g_tls_database_lookup_certificate_issuer() for more information.
275 	 *
276 	 * Params:
277 	 *     certificate = a #GTlsCertificate
278 	 *     interaction = used to interact with the user if necessary
279 	 *     flags = flags which affect the lookup operation
280 	 *     cancellable = a #GCancellable, or %NULL
281 	 *     callback = callback to call when the operation completes
282 	 *     userData = the data to pass to the callback function
283 	 *
284 	 * Since: 2.30
285 	 */
286 	public void lookupCertificateIssuerAsync(TlsCertificate certificate, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
287 	{
288 		g_tls_database_lookup_certificate_issuer_async(gTlsDatabase, (certificate is null) ? null : certificate.getTlsCertificateStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
289 	}
290 
291 	/**
292 	 * Finish an asynchronous lookup issuer operation. See
293 	 * g_tls_database_lookup_certificate_issuer() for more information.
294 	 *
295 	 * Params:
296 	 *     result = a #GAsyncResult.
297 	 *
298 	 * Returns: a newly allocated issuer #GTlsCertificate,
299 	 *     or %NULL. Use g_object_unref() to release the certificate.
300 	 *
301 	 * Since: 2.30
302 	 *
303 	 * Throws: GException on failure.
304 	 */
305 	public TlsCertificate lookupCertificateIssuerFinish(AsyncResultIF result)
306 	{
307 		GError* err = null;
308 
309 		auto __p = g_tls_database_lookup_certificate_issuer_finish(gTlsDatabase, (result is null) ? null : result.getAsyncResultStruct(), &err);
310 
311 		if (err !is null)
312 		{
313 			throw new GException( new ErrorG(err) );
314 		}
315 
316 		if(__p is null)
317 		{
318 			return null;
319 		}
320 
321 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p, true);
322 	}
323 
324 	/**
325 	 * Look up certificates issued by this issuer in the database.
326 	 *
327 	 * This function can block, use g_tls_database_lookup_certificates_issued_by_async() to perform
328 	 * the lookup operation asynchronously.
329 	 *
330 	 * Params:
331 	 *     issuerRawDn = a #GByteArray which holds the DER encoded issuer DN.
332 	 *     interaction = used to interact with the user if necessary
333 	 *     flags = Flags which affect the lookup operation.
334 	 *     cancellable = a #GCancellable, or %NULL
335 	 *
336 	 * Returns: a newly allocated list of #GTlsCertificate
337 	 *     objects. Use g_object_unref() on each certificate, and g_list_free() on the release the list.
338 	 *
339 	 * Since: 2.30
340 	 *
341 	 * Throws: GException on failure.
342 	 */
343 	public ListG lookupCertificatesIssuedBy(ByteArray issuerRawDn, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable)
344 	{
345 		GError* err = null;
346 
347 		auto __p = g_tls_database_lookup_certificates_issued_by(gTlsDatabase, (issuerRawDn is null) ? null : issuerRawDn.getByteArrayStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
348 
349 		if (err !is null)
350 		{
351 			throw new GException( new ErrorG(err) );
352 		}
353 
354 		if(__p is null)
355 		{
356 			return null;
357 		}
358 
359 		return new ListG(cast(GList*) __p, true);
360 	}
361 
362 	/**
363 	 * Asynchronously look up certificates issued by this issuer in the database. See
364 	 * g_tls_database_lookup_certificates_issued_by() for more information.
365 	 *
366 	 * The database may choose to hold a reference to the issuer byte array for the duration
367 	 * of of this asynchronous operation. The byte array should not be modified during
368 	 * this time.
369 	 *
370 	 * Params:
371 	 *     issuerRawDn = a #GByteArray which holds the DER encoded issuer DN.
372 	 *     interaction = used to interact with the user if necessary
373 	 *     flags = Flags which affect the lookup operation.
374 	 *     cancellable = a #GCancellable, or %NULL
375 	 *     callback = callback to call when the operation completes
376 	 *     userData = the data to pass to the callback function
377 	 *
378 	 * Since: 2.30
379 	 */
380 	public void lookupCertificatesIssuedByAsync(ByteArray issuerRawDn, TlsInteraction interaction, GTlsDatabaseLookupFlags flags, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
381 	{
382 		g_tls_database_lookup_certificates_issued_by_async(gTlsDatabase, (issuerRawDn is null) ? null : issuerRawDn.getByteArrayStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
383 	}
384 
385 	/**
386 	 * Finish an asynchronous lookup of certificates. See
387 	 * g_tls_database_lookup_certificates_issued_by() for more information.
388 	 *
389 	 * Params:
390 	 *     result = a #GAsyncResult.
391 	 *
392 	 * Returns: a newly allocated list of #GTlsCertificate
393 	 *     objects. Use g_object_unref() on each certificate, and g_list_free() on the release the list.
394 	 *
395 	 * Since: 2.30
396 	 *
397 	 * Throws: GException on failure.
398 	 */
399 	public ListG lookupCertificatesIssuedByFinish(AsyncResultIF result)
400 	{
401 		GError* err = null;
402 
403 		auto __p = g_tls_database_lookup_certificates_issued_by_finish(gTlsDatabase, (result is null) ? null : result.getAsyncResultStruct(), &err);
404 
405 		if (err !is null)
406 		{
407 			throw new GException( new ErrorG(err) );
408 		}
409 
410 		if(__p is null)
411 		{
412 			return null;
413 		}
414 
415 		return new ListG(cast(GList*) __p, true);
416 	}
417 
418 	/**
419 	 * Determines the validity of a certificate chain, outside the context
420 	 * of a TLS session.
421 	 *
422 	 * @chain is a chain of #GTlsCertificate objects each pointing to the next
423 	 * certificate in the chain by its #GTlsCertificate:issuer property.
424 	 *
425 	 * @purpose describes the purpose (or usage) for which the certificate
426 	 * is being used. Typically @purpose will be set to %G_TLS_DATABASE_PURPOSE_AUTHENTICATE_SERVER
427 	 * which means that the certificate is being used to authenticate a server
428 	 * (and we are acting as the client).
429 	 *
430 	 * The @identity is used to ensure the server certificate is valid for
431 	 * the expected peer identity. If the identity does not match the
432 	 * certificate, %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the
433 	 * return value. If @identity is %NULL, that bit will never be set in
434 	 * the return value. The peer identity may also be used to check for
435 	 * pinned certificates (trust exceptions) in the database. These may
436 	 * override the normal verification process on a host-by-host basis.
437 	 *
438 	 * Currently there are no @flags, and %G_TLS_DATABASE_VERIFY_NONE should be
439 	 * used.
440 	 *
441 	 * If @chain is found to be valid, then the return value will be 0. If
442 	 * @chain is found to be invalid, then the return value will indicate at
443 	 * least one problem found. If the function is unable to determine
444 	 * whether @chain is valid (for example, because @cancellable is
445 	 * triggered before it completes) then the return value will be
446 	 * %G_TLS_CERTIFICATE_GENERIC_ERROR and @error will be set accordingly.
447 	 * @error is not set when @chain is successfully analyzed but found to
448 	 * be invalid.
449 	 *
450 	 * GLib guarantees that if certificate verification fails, at least one
451 	 * error will be set in the return value, but it does not guarantee
452 	 * that all possible errors will be set. Accordingly, you may not safely
453 	 * decide to ignore any particular type of error. For example, it would
454 	 * be incorrect to mask %G_TLS_CERTIFICATE_EXPIRED if you want to allow
455 	 * expired certificates, because this could potentially be the only
456 	 * error flag set even if other problems exist with the certificate.
457 	 *
458 	 * Prior to GLib 2.48, GLib's default TLS backend modified @chain to
459 	 * represent the certification path built by #GTlsDatabase during
460 	 * certificate verification by adjusting the #GTlsCertificate:issuer
461 	 * property of each certificate in @chain. Since GLib 2.48, this no
462 	 * longer occurs, so you cannot rely on #GTlsCertificate:issuer to
463 	 * represent the actual certification path used during certificate
464 	 * verification.
465 	 *
466 	 * Because TLS session context is not used, #GTlsDatabase may not
467 	 * perform as many checks on the certificates as #GTlsConnection would.
468 	 * For example, certificate constraints may not be honored, and
469 	 * revocation checks may not be performed. The best way to verify TLS
470 	 * certificates used by a TLS connection is to let #GTlsConnection
471 	 * handle the verification.
472 	 *
473 	 * The TLS backend may attempt to look up and add missing certificates
474 	 * to the chain. This may involve HTTP requests to download missing
475 	 * certificates.
476 	 *
477 	 * This function can block. Use g_tls_database_verify_chain_async() to
478 	 * perform the verification operation asynchronously.
479 	 *
480 	 * Params:
481 	 *     chain = a #GTlsCertificate chain
482 	 *     purpose = the purpose that this certificate chain will be used for.
483 	 *     identity = the expected peer identity
484 	 *     interaction = used to interact with the user if necessary
485 	 *     flags = additional verify flags
486 	 *     cancellable = a #GCancellable, or %NULL
487 	 *
488 	 * Returns: the appropriate #GTlsCertificateFlags which represents the
489 	 *     result of verification.
490 	 *
491 	 * Since: 2.30
492 	 *
493 	 * Throws: GException on failure.
494 	 */
495 	public GTlsCertificateFlags verifyChain(TlsCertificate chain, string purpose, SocketConnectableIF identity, TlsInteraction interaction, GTlsDatabaseVerifyFlags flags, Cancellable cancellable)
496 	{
497 		GError* err = null;
498 
499 		auto __p = g_tls_database_verify_chain(gTlsDatabase, (chain is null) ? null : chain.getTlsCertificateStruct(), Str.toStringz(purpose), (identity is null) ? null : identity.getSocketConnectableStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
500 
501 		if (err !is null)
502 		{
503 			throw new GException( new ErrorG(err) );
504 		}
505 
506 		return __p;
507 	}
508 
509 	/**
510 	 * Asynchronously determines the validity of a certificate chain after
511 	 * looking up and adding any missing certificates to the chain. See
512 	 * g_tls_database_verify_chain() for more information.
513 	 *
514 	 * Params:
515 	 *     chain = a #GTlsCertificate chain
516 	 *     purpose = the purpose that this certificate chain will be used for.
517 	 *     identity = the expected peer identity
518 	 *     interaction = used to interact with the user if necessary
519 	 *     flags = additional verify flags
520 	 *     cancellable = a #GCancellable, or %NULL
521 	 *     callback = callback to call when the operation completes
522 	 *     userData = the data to pass to the callback function
523 	 *
524 	 * Since: 2.30
525 	 */
526 	public void verifyChainAsync(TlsCertificate chain, string purpose, SocketConnectableIF identity, TlsInteraction interaction, GTlsDatabaseVerifyFlags flags, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
527 	{
528 		g_tls_database_verify_chain_async(gTlsDatabase, (chain is null) ? null : chain.getTlsCertificateStruct(), Str.toStringz(purpose), (identity is null) ? null : identity.getSocketConnectableStruct(), (interaction is null) ? null : interaction.getTlsInteractionStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
529 	}
530 
531 	/**
532 	 * Finish an asynchronous verify chain operation. See
533 	 * g_tls_database_verify_chain() for more information.
534 	 *
535 	 * If @chain is found to be valid, then the return value will be 0. If
536 	 * @chain is found to be invalid, then the return value will indicate
537 	 * the problems found. If the function is unable to determine whether
538 	 * @chain is valid or not (eg, because @cancellable is triggered
539 	 * before it completes) then the return value will be
540 	 * %G_TLS_CERTIFICATE_GENERIC_ERROR and @error will be set
541 	 * accordingly. @error is not set when @chain is successfully analyzed
542 	 * but found to be invalid.
543 	 *
544 	 * Params:
545 	 *     result = a #GAsyncResult.
546 	 *
547 	 * Returns: the appropriate #GTlsCertificateFlags which represents the
548 	 *     result of verification.
549 	 *
550 	 * Since: 2.30
551 	 *
552 	 * Throws: GException on failure.
553 	 */
554 	public GTlsCertificateFlags verifyChainFinish(AsyncResultIF result)
555 	{
556 		GError* err = null;
557 
558 		auto __p = g_tls_database_verify_chain_finish(gTlsDatabase, (result is null) ? null : result.getAsyncResultStruct(), &err);
559 
560 		if (err !is null)
561 		{
562 			throw new GException( new ErrorG(err) );
563 		}
564 
565 		return __p;
566 	}
567 }